package controller

import (
	"net/http"
	"project/PersonalBlog/model"
	"project/PersonalBlog/service"
	"strconv"

	"github.com/gin-gonic/gin"
)

//IndexHandler ..默认加载模板
func IndexHandler(c *gin.Context) {
	//加载文章数据
	articles := service.GetArticleRecord(10)
	if articles == nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	categorys := service.GetAllCategoryList()
	if categorys == nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	// var data map[string]interface{} = make(map[string]interface{}, 16)
	// data["article_list"] = articles
	// data["category_list"] = categorys
	c.HTML(http.StatusOK, "views/index.html", gin.H{
		"article_list":  articles,
		"category_list": categorys,
	})
}

//CategoryList ..加载分类文章
func CategoryList(c *gin.Context) {
	sid := c.Query("category_id")
	id, err := strconv.ParseInt(sid, 10, 32)
	if err != nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	articles := service.GetCategoryArticle(int32(id))
	if articles == nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	categorys := service.GetAllCategoryList()
	if categorys == nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	c.HTML(http.StatusOK, "views/index.html", gin.H{
		"article_list":  articles,
		"category_list": categorys,
	})
}

//ReadDetailHandler ..阅读原文
func ReadDetailHandler(c *gin.Context) {
	sid := c.Query("article_id")
	id, err := strconv.ParseInt(sid, 10, 64)
	if err != nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	articles := service.GetArticleContext(id)
	if articles == nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	//articles.Content = template.HTML(articles.Content)
	//相关文章
	xarticles := service.GetCategoryArticle(int32(articles.ArticleInfo.CategoryID))
	if xarticles == nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	articPN := service.GetArticlePN(articles.ID)
	categorys := service.GetAllCategoryList()
	if categorys == nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	//获取评论
	comments := service.GetComments(articles.ID)
	//显示文章详细内容
	c.HTML(http.StatusOK, "views/detail.html", gin.H{
		"article_id":       articles.ID,
		"detail":           articles,
		"relative_article": xarticles,
		"comment_list":     comments,
		"category":         categorys,
		"prev":             articPN[0],
		"next":             articPN[1],
	})
}

//PublishedComment ..插入评论
func PublishedComment(c *gin.Context) {
	com := &model.Comment{}
	sid := c.PostForm("article_id")
	id, err := strconv.ParseInt(sid, 10, 64)
	if err != nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	com.ArticleID = id
	com.Content = c.PostForm("comment")
	com.UserName = c.PostForm("author")
	com.Email = c.PostForm("email")
	if service.InputComments(com) < 0 {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	c.Redirect(http.StatusMovedPermanently, "/article/detail/?article_id="+sid)
}

//EditorArticle ..投稿
func EditorArticle(c *gin.Context) {
	categorys := service.GetAllCategoryList()
	if categorys == nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	c.HTML(http.StatusOK, "views/post_article.html", gin.H{
		"categorys": categorys,
	})
}

//PublishedArticle ..提交文章
func PublishedArticle(c *gin.Context) {
	article := &model.ArticleDetail{}
	article.UserName = c.PostForm("author")
	article.Title = c.PostForm("title")
	article.Content = c.PostForm("content")
	sid := c.PostForm("category_id")
	id, err := strconv.ParseInt(sid, 10, 64)
	if err != nil {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	article.CategoryID = id
	if i := service.InputArticle(article); i > 0 {
		aid := strconv.FormatInt(i, 10)
		//c.String(http.StatusOK, fmt.Sprintf(`uploaded:"true",url:"/article/detail/?article_id="`)+aid)
		c.Redirect(http.StatusMovedPermanently, "/article/detail/?article_id="+aid)
		return
	}
	c.HTML(http.StatusInternalServerError, "views/500.html", nil)
}

//AboutMe ..
func AboutMe(c *gin.Context) {
	c.HTML(http.StatusOK, "views/about.html", nil)
}

//PublishedLeavel ..留言
func PublishedLeavel(c *gin.Context) {
	com := &model.Comment{}
	com.Content = c.PostForm("comment")
	com.UserName = c.PostForm("author")
	com.Email = c.PostForm("email")
	if service.InputLeaves(com) < 0 {
		c.HTML(http.StatusInternalServerError, "views/500.html", nil)
	}
	c.Redirect(http.StatusMovedPermanently, "/leave/new/")
}

//ReadLeaves ..查看留言
func ReadLeaves(c *gin.Context) {
	leave := service.GetLeaves()
	c.HTML(http.StatusMovedPermanently, "views/gbook.html", leave)
}
